﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ZFine.Code
{
    public class MessageSend
    {
        private static string _username = "";
        private static string _passwd = "";
        private static bool _iscansend = true;
        //配置短信帐号短信账户：zhongdai   密码456456
        private string username
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_username))
                {
                    _username = ConfigurationManager.AppSettings.Get("NoteID");
                    if (string.IsNullOrWhiteSpace(_username))
                    {
                        _username = "xianli88";
                    }
                }
                return _username;
            }
        }
        //配置短信密码
        private string passwd
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_passwd))
                {
                    _passwd = ConfigurationManager.AppSettings.Get("NotePass");
                    if (string.IsNullOrWhiteSpace(_passwd))
                    {
                        _passwd = "xianli168";
                    }
                }
                return _passwd;
            }
        }
        private bool iscansend
        {
            get
            {
                string iscansendsetting = ConfigurationManager.AppSettings.Get("IsCanSend");
                if (!string.IsNullOrWhiteSpace(iscansendsetting))
                {
                    _iscansend = iscansendsetting.ToBool();
                }
                return _iscansend;
            }
        }

        /**
         * 发送短信
         * phone 为手机号码，多个号码以","号隔开
         * msg 为短信内容，须加上签名，例如"【微软科技】"，短信内容为utf-8编码，有特殊符号请urlencode内容
         * needstatus 是否需要推送短信 值为true或false;
         * port 端口，默认为空
         * sendtime 发送时间，为空即马上发送，格式例如："2016-12-12 12:12:12" 这样的标准格式
         **/
        public String sendMsg(String phone, String msg, String needstatus, String port, String sendtime)
        {
            if (!iscansend)
            {
                return "{\"batchno\":\"0719090433952547767\",\"respcode\":\"0\",\"respdesc\":\"短信发送成功\"}";
            }
            String url = "http://www.qybor.com:8500/shortMessage?username=" + this.username + "&passwd=" + this.passwd + "&phone=" + phone + "&msg=" + msg + "&needstatus=" + needstatus + "&port=" + port + "&sendtime=" + sendtime;
            string strRet = "";
            string targeturl = url.Trim().ToString();
            try
            {
                HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl);
                hr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
                hr.Method = "GET";
                hr.Timeout = 30 * 60 * 1000;
                WebResponse hs = hr.GetResponse();
                Stream sr = hs.GetResponseStream();
                StreamReader ser = new StreamReader(sr, Encoding.UTF8);
                strRet = ser.ReadToEnd();
            }
            catch (Exception ex)
            {
                strRet = ex.ToString();
            }
            return strRet;
        }
        /**
         * 短信发送
         * phone 号码
         * msg 内容
         **/
        public String send(String phone, String msg)
        {
            return this.sendMsg(phone, msg, "true", "", "");
        }
    }
}
